package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;

public class BibliotecaApp {

    private Library library = new Library();
    private UserInteraction userInteraction = new UserInteraction();
    static boolean menuUpdated = false;
    List<MenuItem> menuItemList = new ArrayList<MenuItem>() {
        {
            add(new ListBook(library));
            add(new ListMovie(library));
            add(new LoginValidator(userInteraction));
            add(new Quit());
        }};

    List<MenuItem> menuItemListAfterLogin = new ArrayList<MenuItem>() {{
        add(new ListBook(library));
        add(new ListMovie(library));
        add(new CheckOutBook(library));
        add(new CheckOutMovie(library));
        add(new ReturnBook(library));
        add(new ReturnMovie(library));
        add(new DisplayUserInformation());
        add(new LogOut());
        add(new Quit());
    }};

    public static void main(String[] args) {

        BibliotecaApp bibliotecaApp = new BibliotecaApp();
        Boolean toContinue = true;
        bibliotecaApp.userInteraction.displayResultString("Welcome to Biblioteca");

        while ( toContinue) {


            MenuItem menuItem;
            MenuActionResult menuActionResult = new MenuActionResult();
            String menuToDisplay = new String();

            bibliotecaApp.userInteraction.displayResultString("\n\t\t\t\tMenu\n\t\t\t\t====");

            try {

                int choice;
                Menu menu;
                if ( LoginValidator.getCurrentUser() != null) {
                    menu = new Menu(bibliotecaApp.menuItemListAfterLogin);
                } else {
                    menu = new Menu(bibliotecaApp.menuItemList);
                }
                menu.displayMenu(bibliotecaApp.userInteraction);
                choice = bibliotecaApp.userInteraction.getMenuOption();
                menuItem = menu.getMenuItemForOption(choice);

                if (menuItem != null)
                    menuActionResult = menuItem.performMenuAction(bibliotecaApp.userInteraction);
                else
                    bibliotecaApp.userInteraction.displayResultString("Please enter a valid input!!");

                if ( menuItem instanceof Quit && (menuActionResult.getState() == true))
                    toContinue = false;

            } catch (Exception e) {
                bibliotecaApp.userInteraction.displayResultString("Exception in input!");
            }

        }

    }

}
